package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.AdviceMapper;
import com.jt.pojo.Advice;
import com.jt.pojo.Item;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class AdviceServiceImpl implements AdviceService {
    @Autowired
    private AdviceMapper adviceMapper;




    @Override
    public void addAdvice(Advice advice) {
        adviceMapper.addAdvice(advice);
    }

    @Override
    public void deleteAdviceById(Integer id) {
        adviceMapper.deleteAdviceById(id);
    }

    @Override
    public Advice findAdviceById(Integer id) {
        return adviceMapper.findAdviceById(id);


    }

    @Override
    public void updateAdvice(Advice advice) {
        advice.setUpdated(new Date());
        adviceMapper.updateAdvice(advice);
    }

    @Override
    public PageResult findAdviceList(PageResult pageResult) {
        //1,获取总记录数
        QueryWrapper<Advice> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("result", pageResult.getQuery());

        //2,完成分页操作
        //2.1 准备一个分页对象,MP基于初始化条件，获取分页其他数据
        IPage<Advice> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        page = adviceMapper.selectPage(page, queryWrapper);
        //2,2获取总数，获取记录数
        long total = page.getTotal();
        List<Advice> list = page.getRecords();
        return pageResult.setTotal(total).setRows(list);
    }




}